package com.tgy.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class _331_验证二叉树的前序序列化 {

    public static boolean isValidSerialization(String preorder) {



        return false;
    }

    public static boolean isValidSerialization01(String preorder) {

        if (preorder == null || preorder.length() == 0) {

            return false;
        }

        String[] vals = preorder.split(",");
        LinkedList<String> valsList = new LinkedList<>(Arrays.asList(vals));
        boolean flag = traversal(valsList);

        if (flag) {
            flag = valsList.isEmpty();
        }

        return flag;
    }

    private static boolean traversal(LinkedList<String> valsList) {

        if (valsList.isEmpty()) {

            return false;
        }
        String val = valsList.removeFirst();
        if (val.equals("#")) {
            return true;
        }
        boolean flag = traversal(valsList);

        if (!flag) {
            return false;
        }

        return traversal(valsList);
    }

    public static void main(String[] args) {

//        String preorder = "9,#,#,1";
//        String preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#";
        String preorder = "1,#";
        System.out.println(isValidSerialization(preorder));
    }


}
